﻿using System;
using System.Collections.Generic;
using System.Web;
using BaseData;
using BaseData.UI;
using BaseData.Common;

namespace Shizir.Base
{
    public class BasePage : BaseData.UI.BasePage
    {
        /// <summary>
        /// 用户编号
        /// </summary>
        protected int userID = 0;
        /// <summary>
        /// 用户名
        /// </summary>
        protected string userName = string.Empty;
        /// <summary>
        /// 用户类别（权限）
        /// </summary>
        protected UserType userType = UserType.Agent;
        /// <summary>
        /// 用户基类
        /// </summary>
        protected ShiziEntity.User.UserBase user = null;
        /// <summary>
        /// 文件版本号
        /// </summary>
        public static string FileVersion = GetConfig("FileVersion");
        /// <summary>
        /// 加密密钥
        /// </summary>
        private static string encryptKey = "shizir";

        public void init()
        {
            string redirecturl = "/Default.aspx"; 
            string password = string.Empty;

            if (Request.Cookies["loginid"] != null && Request.Cookies["loginid"].Value != string.Empty
                && Request.Cookies["loginusername"] != null && Request.Cookies["loginusername"].Value != string.Empty
                && Request.Cookies["loginpassword"] != null && Request.Cookies["loginpassword"].Value != string.Empty
                && Request.Cookies["loginvalidation"] != null && Request.Cookies["loginvalidation"].Value != string.Empty)
            {
                int.TryParse(Request.Cookies["loginid"].Value, out userID);
                userName = Server.UrlDecode(Request.Cookies["loginusername"].Value);
                password = DESEncrypt.Decrypt(Request.Cookies["agent_loginpwd"].Value, encryptKey);
                string validation = Request.Cookies["loginvalidation"].Value;
                if (validation == EncryptCookie(userName, userID.ToString()))
                {
                    if (Request.Cookies["usertype"] != null && Request.Cookies["usertype"].Value != string.Empty)
                    {
                        userType = (UserType)Enum.Parse(typeof(UserType), Request.Cookies["usertype"].Value, true);
                    }
                    UserloginCookie(userName, password, userID.ToString(), userType);
                }
                else
                {
                    userID = 0;
                    userName = string.Empty;
                }
            }

            if (userID > 0)
            {
                //user = 
                if (user != null && user.UserID > 0)
                {
                    userType = user.GetUserType();
                }
                else
                {
                    Jscript.AlertAndRedirectParent("请先登录！", redirecturl);
                    Response.End();
                }
            }
            else
            {
                Jscript.AlertAndRedirectParent("请先登录！", redirecturl);
                Response.End();
            }
        }

        /// <summary>
        /// 加密用户名和用户编号
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="userID">用户编号</param>
        /// <returns>加密字符串</returns>
        public static string EncryptCookie(string userName, string userID)
        {
            int length = userName.Length;
            string encrypt = userName.Substring(0, length - 1) + "囧" + userID + userName.Substring(length - 1);
            string key = "stonny";
            return DESEncrypt.Encrypt(encrypt, key);
        }

        /// <summary>
        /// 写用户登录Cookie
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="userID">用户ID</param>
        /// <param name="password">用户密码</param>
        /// <param name="userType">用户权限</param>
        public static void UserloginCookie(string userName, string userID, string password, UserType userType)
        {
            string cookieDomain = GetConfig("Domain");
            DateTime cookieDate = DateTime.Now.AddHours(12);
            //userName
            System.Web.HttpContext.Current.Response.Cookies["loginname"].Value = System.Web.HttpContext.Current.Server.UrlEncode(userName);
            System.Web.HttpContext.Current.Response.Cookies["loginname"].Domain = cookieDomain;
            System.Web.HttpContext.Current.Response.Cookies["loginusername"].Value = System.Web.HttpContext.Current.Server.UrlEncode(userName);
            System.Web.HttpContext.Current.Response.Cookies["loginusername"].Domain = cookieDomain;
            HttpContext.Current.Response.Cookies["loginusername"].Expires = cookieDate;
            //userID
            System.Web.HttpContext.Current.Response.Cookies["loginid"].Value = System.Web.HttpContext.Current.Server.UrlEncode(userID);
            System.Web.HttpContext.Current.Response.Cookies["loginid"].Domain = cookieDomain;
            //password
            System.Web.HttpContext.Current.Response.Cookies["loginpassword"].Value = DESEncrypt.Encrypt(password, encryptKey);
            System.Web.HttpContext.Current.Response.Cookies["loginpassword"].Domain = cookieDomain;
            //写userType
            HttpContext.Current.Response.Cookies["usertype"].Value = ((int)userType).ToString();
            HttpContext.Current.Response.Cookies["usertype"].Domain = cookieDomain;
            HttpContext.Current.Response.Cookies["usertype"].Expires = cookieDate;
            //validation
            System.Web.HttpContext.Current.Response.Cookies["loginvalidation"].Value = EncryptCookie(userName, userID);
            System.Web.HttpContext.Current.Response.Cookies["loginvalidation"].Domain = cookieDomain;
        }
    }
}